/*
 * TotalEquilibriumNewtonSolver.h
 *
 *  Created on: 4 Sep 2011
 *      Author: Allan
 */

#ifndef TOTALEQUILIBRIUMNEWTONSOLVER_H_
#define TOTALEQUILIBRIUMNEWTONSOLVER_H_

// C++ includes
#include <vector>
using namespace std;

// Eigen includes
#include <Eigen/Core>
using namespace Eigen;

// GeoReact includes
#include "TotalEquilibriumNewtonProblem.h"
#include "Core/Multiphase.h"
#include "Core/ReactionSystem.h"

class TotalEquilibriumNewtonSolver
{
public:
	struct Diagnostic
	{ unsigned iterations; bool converged; };
	
public:
	TotalEquilibriumNewtonSolver();
	
	TotalEquilibriumNewtonSolver(const Multiphase& multiphase, const ReactionSystem& eReactions, const vector<string>& jSpecies, const vector<string>& eSpecies);
	
	void Initialiaze(const Multiphase& multiphase, const ReactionSystem& eReactions, const vector<string>& jSpecies, const vector<string>& eSpecies);
	
	void SetSolverOptions(double tolerance, unsigned maxiterations);
	
	const Diagnostic Solve(const VectorXd& uj, double T, double P, VectorXd& n);
	
private:
	/// The total equilibrium problem object which overloads the methods Function and Jacobian from MultirootProblem 
	TotalEquilibriumNewtonProblem problem;
	
	/// The multiroot solver object used to solve the non-linear system of equations
	NewtonSolver solver;
	
	/// The number of primary (Nj) and equilibrium (Ne) species
	unsigned Nj, Ne;
	
	/// The indexes of the primary (jIndexes) and equilibrium (eIndexes) species
	vector<Index> jIndexes, eIndexes;
	
	/// The tolerance for the iterative procedure
	double tolerance;
	
	/// The maximum number of iterations for the iterative procedure
	unsigned maxiterations;
};

#endif /* TOTALEQUILIBRIUMNEWTONSOLVER_H_ */
